<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <!-- 新增按钮 -->
        <div style="text-align: right; margin-bottom: 20px">
          <!-- 如果是线上接口 pid传空串 -->
          <el-button type="primary" size="small" @click="addPermission(1,'')">添加权限</el-button>
        </div>
        <!-- table -->
        <el-table :data="list" border row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <!-- 加一个判断 只有是一级才能显示添加按钮 -->
              <el-button v-if="row.type === 1" type="text" @click="addPermission(2,row.id)">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!--
      新增弹框
     -->
    <el-dialog :visible="showDialog" title="弹层标题" @close="closeDialog">
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="form.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button>取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { transTree } from '@/utils/transTreeData'
export default {
  data() {
    return {
      list: [], // 权限点列表
      showDialog: false,
      form: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  },
  mounted() {
    this.hGetList()
  },
  methods: {
    async hGetList() {
      const res = await getPermissionList()
      this.list = transTree(res.data)
    },
    // 添加方法
    addPermission(type, id) {
      this.form.type = type
      this.form.pid = id
      this.showDialog = true
    },
    // 确定按钮
    async confirm() {
      // 做判断  根据是否有id 调用不同接口
      if (this.form.id) {
        // 调用编辑接口
        await updatePermission(this.form)
      } else {
        // 调用新增接口
        await addPermission(this.form)
      }
      this.$message({
        type: 'success',
        message: `${this.form.id ? '编辑' : '新增'}${this.form.type === 1 ? '一级' : '二级'}权限点成功`
      })
      // 更新列表
      this.hGetList()
      this.closeDialog()
    },
    closeDialog() {
      this.showDialog = false
      this.form = {
        enVisible: '0',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: ''
      }
    },
    delPermission(id) {
      this.$confirm('确定要删除该权限吗?', '温馨提示').then(async() => {
        // 调用接口
        await delPermission(id)
        // 更新列表
        this.hGetList()
        this.$message.success('删除成功')
      }).catch(() => {
        console.log('取消')
      })
    },
    async editPermission(id) {
      // 回填操作
      const res = await getPermissionDetail(id)
      // this.form
      this.form = res.data
      // 打开弹框
      this.showDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.tips {
    line-height: 34px;
    padding: 0px 15px;
    border-radius: 5px;
    border: 1px solid rgba(145, 213, 255, 1);
    background: rgba(230, 247, 255, 1);
    i {
      margin-right: 10px;
      color: #409eff;
    }
  }
</style>
